#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "src/DataControl/InputControl.h"
#include <QDebug>
#include <QFileDialog>


MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);

    GOOGLE_PROTOBUF_VERIFY_VERSION;

    connect(&updateProgress, &QTimer::timeout, this,[=](){
        ui->progressBar->setValue(control.progress);
        while (!control.log.empty()) {
            ui->log->append(control.log.front());
            control.log.pop_front();
        }
        ui->log->moveCursor(QTextCursor::End);
        if(ui->progressBar->value() == 100){
            //ui->log->append("*****************解码完成!*********************");
            updateProgress.stop();
        }
    });
}

MainWindow::~MainWindow()
{
    google::protobuf::ShutdownProtobufLibrary();
    delete ui;
}


void MainWindow::on_decode_clicked()
{
    DecodeControl f;
    bool ret =f.readFromLocal("Gen/Out.gen");

    qDebug()<<"ret:"<<ret;
}

void MainWindow::on_creat_clicked()
{
    auto h = InputControl::TestCreat();
    InputControl::CreatSerial(h);
}

void MainWindow::on_openOUT_clicked()
{
    QString fileName = QFileDialog::getOpenFileName(this,
                                                    "/",
                                                    tr("Open Image"), tr("gen Files (*.gen *.txt *.fd)"));
    ui->input_Out->setText(fileName);
}

void MainWindow::on_openDB_clicked()
{
    QString fileName = QFileDialog::getOpenFileName(this,
                                                    "",
                                                    tr("Open Image"), tr("DB Files (*.db)"));
    ui->input_DB->setText(fileName);
}

void MainWindow::on_insertSQL_clicked()
{
    if(ui->input_DB->text().isEmpty() || ui->input_Out->text().isEmpty()){
        qDebug()<<"Not Input";
    }else {

        QString format=ui->input_Out->text().section(".",-1);
        if("gen" == format){
            _format=GG_GEN;
            _fun_Ptr = std::bind(&DecodeControl::readFromLocal, &control,std::placeholders::_1);
            std::cout<<"IO STRAM解码";
        }else if ("fd" == format) {
            _format=GG_FD;
            _fun_Ptr = std::bind(&DecodeControl::readFromLocalFd, &control,std::placeholders::_1);
            std::cout<<"FD解码";
        }else if ("txt" == format) {
            _format=GG_STRING;
            _fun_Ptr = std::bind(&DecodeControl::readFromLocalTxt, &control,std::placeholders::_1);
            std::cout<<"STRING解码";
        }else  {
            _format=GG_OHTER;
            _fun_Ptr = std::bind(&DecodeControl::readFromLocalTxt, &control,std::placeholders::_1);
            std::cout<<"DEFAULT解码";
        }

        updateProgress.start(50);
        std::thread([this]{
            std::cout<<_fun_Ptr(ui->input_Out->text().toStdString())<<std::endl;
            qDebug()<<"准备数据库:"<<control.insert2SQL(ui->input_DB->text().toStdString());
            qDebug()<<"数据库解码:"<<control.insertProtoData();
            control.closeDB();
        }).detach();
    }
}
